package comp3021.a1;

/***
 * Batch Mode --> batchOrInt = false
 * Interactive Mode --> batchOrInt = true
 */

public class POSMain extends Configuration{
	   
	   public static void main(String[] args) throws Exception 
	   {	
		   System.out.println("Welcome to the Point-Of-Sale Registration System");
		   if(args.length==0)		//interactive mode
		   {
			  BatchOrInt = false;
		   }
		   else						//batchMode
		   {	
			   BatchOrInt = true;
			   try{
				   java.io.File file= new java.io.File(args[0]);
				   batch = new java.util.Scanner(file);
			   }
			   catch (Exception illegalInputFile){ System.out.println("Batch Mode File does not exist!"); };
		   }
			   do{
			   System.out.printf("Please enter your user name: ");
			   		try{	
			   			if(BatchOrInt){
			   				inputUser= batch.next();
			   				System.out.println(inputUser);
			   			}
			   			else{
			   				inputUser  = userInput.next();
			   			}
			   			correctUserName=  users.loginByName(inputUser);
			   			if(! correctUserName )// user inputs the wrong username
			   			{
			   				logbook.error("Non-existent user "+ inputUser +" , please enter again! ");
			   				if(BatchOrInt) 
			   					{
			   					logbook.errorRecord("Non-existent user "+ inputUser +" , please enter again! ");
			   					System.exit(-1);
			   					}
			   			}
			   		}
			   		catch(Exception IO){
			   			System.out.println("Cannot accept other format user name");
			   		}
			   }while(correctUserName == false);							//makesure the user is using the correct username
			   
			   do{
			   System.out.printf("Please enter your password: ");
			   	try{
				   	if(BatchOrInt){
	   				inputPw= batch.next();
	   				System.out.println(inputPw);
	   				}
	   				else{
	   					inputPw = userInput.next();
		   			}
			   	}
			   	catch(Exception IO){
		   		System.out.println("Cannot accept other format user name");
		   		}
					correctPw = users.loginByPw(inputPw);
					if(!correctPw)
					{
					logbook.error("Wrong password " + inputPw+ " for user " + inputUser +" , please enter again!");
					if(BatchOrInt) {
						logbook.errorRecord("Wrong password " + inputPw+ " for user " + inputUser +" , please enter again!");
						System.exit(-1);
					}
					}
					else{
					logbook.error("Electronic-Sales Counter is started successfully by user "+inputUser+"!");
					}
			   }while(correctPw == false);			//	//makesure the user is using the correct password 
			 
			   if(! users.Status()) System.exit(-1);//if the user is not logged in
				calculator();		//entering the System
			   
				
				if(BatchOrInt) batch.close();
				else	userInput.close();
		   }
	public static void calculator() {
		//go to calculator
		  Counter cal = new Counter();
		  cal.mode();
	}  
}